﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls

Partial Class askSheldon
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindQuestions()
    End Sub

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        AddHandler rptPages.ItemCommand, AddressOf rptPages_ItemCommand
    End Sub


    Protected Sub BindQuestions()
        Dim conn As SqlConnection
        Dim dataSet As New DataSet
        Dim adapter As SqlDataAdapter
        Dim connectionString As String
        Dim objPds As PagedDataSource
        objPds = New PagedDataSource()
        connectionString = ConfigurationManager.ConnectionStrings("Brown24").ConnectionString
        conn = New SqlConnection(connectionString)
        conn.Open()
        adapter = New SqlDataAdapter("SELECT AskSheldon.QuestionID, AskSheldon.UserID, AskSheldon.Question, AskSheldon.Answer, AskSheldon.Active, Users.FirstName, Users.LastName, Users.City, Users.State FROM AskSheldon INNER JOIN Users  ON AskSheldon.UserID = Users.UserID WHERE AskSheldon.Active = 1 ORDER BY QuestionDate DESC", conn)

        adapter.Fill(dataSet, "Questions")


        objPds.DataSource = dataSet.Tables(0).DefaultView
        objPds.AllowPaging = True
        objPds.PageSize = 10
        objPds.CurrentPageIndex = CurrentPage

        If objPds.PageCount > 1 Then
            rptPages.Visible = True
            Dim pages As New ArrayList()

            For i As Integer = 0 To objPds.PageCount - 1
                pages.Add((i + 1).ToString())
            Next

            rptPages.DataSource = pages
            rptPages.DataBind()
        Else
            rptPages.Visible = False
        End If

        'lblCurrentPage.Text = "Page: " & (CurrentPage + 1).ToString() & " of " & objPds.PageCount.ToString()
        lblCurrentPage.Text = (objPds.CurrentPageIndex + 1).ToString()
        lblPageCount.Text = objPds.PageCount.ToString()
        lblCount.Text = objPds.DataSourceCount.ToString()

        cmdPrev.Visible = Not objPds.IsFirstPage
        cmdNext.Visible = Not objPds.IsLastPage



        newsRep.DataSource = objPds
        newsRep.DataBind()


        'newsRep.DataBind()

        conn.Close()
    End Sub

    Private Sub rptPages_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
        CurrentPage = Convert.ToInt32(e.CommandArgument) - 1
        BindQuestions()
    End Sub

    Public Property CurrentPage() As Integer
        Get
            ' look for current page in ViewState
            Dim o As Object = Me.ViewState("_CurrentPage")
            If o Is Nothing Then
                Return 0
            Else
                Return CInt(o)
                ' default page index of 0
            End If
        End Get

        Set(ByVal value As Integer)
            Me.ViewState("_CurrentPage") = value
        End Set
    End Property

    'Protected Sub BindSubnav()
    '    Dim conn As SqlConnection
    '    Dim comm As SqlCommand
    '    Dim reader As SqlDataReader
    '    Dim connectionString As String
    '    connectionString = ConfigurationManager.ConnectionStrings("Brown24").ConnectionString
    '    conn = New SqlConnection(connectionString)
    '    comm = New SqlCommand("SELECT TOP 10 NewsID,ArticleDate FROM News ORDER BY ArticleDate Desc", conn)

    '    conn.Open()
    '    reader = comm.ExecuteReader

    '    newsSubNav.DataSource = reader
    '    newsSubNav.DataBind()

    '    reader.Close()
    '    conn.Close()
    'End Sub


    Protected Sub cmdPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdPrev.Click
        CurrentPage -= 1
        BindQuestions()

    End Sub

    Protected Sub cmdNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNext.Click
        CurrentPage += 1
        BindQuestions()
    End Sub

    Protected Sub rptPages_ItemCommand1(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptPages.ItemCommand
        Dim btn As LinkButton
        btn = CType(e.Item.FindControl("btnPage"), LinkButton)
        btn.CssClass = "activePage"
    End Sub
End Class
